#pragma once

#include "StmtTable.h"

StmtTable::StmtTable() {
	count = 0;
};

void StmtTable::insertStmt(INDEX stmtNo, DESIGN_ENTITY de) {
	stmtTable.insert(std::make_pair<INDEX, DESIGN_ENTITY>(stmtNo, de)); 
	++count;
};

void StmtTable::printStmtTable() 
{
	for ( auto it = stmtTable.begin(); it != stmtTable.end(); ++it )
	{
		cout << it->first << " : " << it->second << endl;
	}
};

string StmtTable::buildPatternB(vector<string> RHST) 
{
	vector<string> tata;
	stack<string> sta;
	for(int i = 0;i<RHST.size();i++)
	{	
		if(RHST.at(i)==")")
		{
			while(sta.top()!="(")
			{
				tata.push_back(sta.top());
				sta.pop();
			}
			std::reverse(tata.begin(),tata.end());
			vector<string> pp;
			for (std::vector<string>::iterator it=tata.begin(); it!=tata.end(); ++it)
			{
				pp.push_back(*it);
			}
			string aa = buildPattern(pp);
			pp.clear();
			sta.pop();
			sta.push(aa);
			tata.clear();
		}
		else
		{
			sta.push(RHST.at(i));
		}
	}
	
	string finalString;
	finalString = "";
	vector<string> rr;
	while(!sta.empty())
	{
		rr.push_back(sta.top());
		sta.pop();
	}
	std::reverse(rr.begin(),rr.end());
	vector<string> vava;
  for (std::vector<string>::iterator it=rr.begin(); it!=rr.end(); ++it)
  {
	  vava.push_back(*it);
  }
  vector<string> poda;
  for(int i =0;i<vava.size();i++)
  {
	  if(vava.at(i).front()=='|'&&vava.at(i).size()!=1)
	  {
		  vava.at(i) = vava.at(i).substr(1,vava.at(i).size()-2);
	  }
	  if(vava.at(i).back()=='|'&&vava.at(i).size()!=1)
	  {
		  vava.at(i) = vava.at(i).substr(0,vava.at(i).size()-1);
	  }
	  poda.push_back(vava.at(i));
  }
  finalString = buildPattern(poda);
  string sasa="";
  for(int i =0;i<finalString.length();i++)
  {
	  if(finalString.at(i)=='|')
	  {
		  sasa.append(1,'|');
		  while(finalString.at(i)=='|'&&i<finalString.length()-1)
		  {
			  ++i;
		  }
		  if(finalString.at(i)!='|'){sasa.append(1,finalString.at(i));}
	  }
	  else
	  {
		  sasa.append(1,finalString.at(i));
	  }
  }
  return sasa;
};


string StmtTable::buildPattern(vector<string> RHST) 
{
	for(int i = 0;i<RHST.size();i++)
	{
		//cout << RHST.at(i) << " ";
	}
	//cout << endl;
	for(int i = 0;i<RHST.size();i++)
	{
		if(RHST.at(i)=="*")
		{
			string temp = RHST.at(i+2);
			RHST.at(i+2) = RHST.at(i);
			RHST.at(i) = temp;
			i=i+3;
		}
	}
	for(int i = 0;i<RHST.size();i++)
	{
		//cout << RHST.at(i) << " ";
	}
	//cout << endl;
	string lastInsertO;
	bool cflag = false;
	for(int i = 0;i<RHST.size();i++)
	{
		if(RHST.at(i)=="+"||RHST.at(i)=="-")
		{
			if(!cflag)
			{
				cflag = true;
				lastInsertO = RHST.at(i);
			}
			else
			{
			string temp;
			temp = RHST.at(i);
			RHST.at(i) = lastInsertO;
			lastInsertO = temp;
			}
		}
	}
	for(int i = 0;i<RHST.size();i++)
	{
		if(RHST.at(i)=="+"||RHST.at(i)=="-")
		{
			RHST.erase(RHST.begin()+i,RHST.begin()+i+2);
			break;
		}
	}
	if(cflag)
	{
	RHST.push_back(lastInsertO);
	RHST.push_back("|");
	}
	for(int i = 0;i<RHST.size();i++)
	{
		//cout << RHST.at(i) << " ";
	}
	//cout << endl;
	string finalString;
	finalString = "";
	for(int i = 0;i<RHST.size();i++)
	{
		finalString.append(RHST.at(i));
	}
	return finalString;
};

int StmtTable::getSize()
{		
	return count;
};

NAME StmtTable::getStmtDE(INDEX ind)
{
	unordered_map<INDEX,NAME>::const_iterator got = stmtTable.find(ind);

	if(got == stmtTable.end())
	{
		return NULL;
	}
	else
	{
		return got->second;
	}
};